<?php
require_once('config.php');
/////////////////////////////////////////////////////////////////////////		GET STUDENT by un
function getStudentByUN($username)
{
	$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die ("Could not execute query");
	$query = "SELECT * FROM student WHERE username ='$username'";
	$result = mysqli_query($con,$query) or die ("Could not get the student information by UN");
	while ($row=mysqli_fetch_row($result))
    {	
		$studentID = $row[0];     
		$_SESSION['studentID'] = $studentID;   
		$firstName = $row[4];     
		$_SESSION['firstName'] = $firstName;   
 	}	
}

/////////////////////////////////////////////////////////////////////////		GET STUDENT INFO
function getStudentInfo($studentID)
{
	$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die ("Could not execute query");
	$query = "SELECT * FROM student WHERE studentID ='$studentID'";
	$result = mysqli_query($con,$query) or die ("Could not get the student information");
	while ($row=mysqli_fetch_row($result))
    {	
		$studentID = $row[0];     
		$_SESSION['studentID'] = $studentID;   
		$firstName = $row[4];     
		$_SESSION['firstName'] = $firstName;   
 	}	
}
///////////////////////////////////////////////////////////////////////////////		GET QUESTION
function getQuestion($currentQuestion)
{
	$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die ("Could not execute query");
	$query = "SELECT * FROM question WHERE questionID ='$currentQuestion'";
	$result = mysqli_query($con,$query) or die ("Could not get the required question");
	return mysqli_fetch_assoc($result);
} 	

///////////////////////////////////////////////////////////////////////////////// GET RESULT
function finalScore($studentID, $attempt)
{
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die ("Could not execute query");
$query = "SELECT finalScore FROM studentintest WHERE studentID ='$studentID' AND attempt='$attempt'";

        $result = mysqli_query($con,$query) or die ("Could not get final score");
       	while ($row = mysqli_fetch_array($result)) 
{
		extract($row);
}		
return $finalScore;	
} 
///////////////////////////////////////////////////////////////////////////////// GET RESULT
function avgFinalScore($studentID, $attempt)
{
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die ("Could not execute query");
$query = "SELECT AVG(finalScore) FROM studentintest WHERE attempt='$attempt'";
$result = mysqli_query($con,$query) or die ("Could not get final avg score");
while ($row = mysqli_fetch_row($result))
    {	
$avgFinal = $row[0];		
 	}	
return round($avgFinal);
} 

//////////////////////////////////////////////////////////////////////////////////// GET TEST FEEDBACK		
function getTestFeedback($studentID, $testID, $attempt)
{
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die ("Could not execute query");
$query = "SELECT * FROM studentresponse WHERE studentID ='$studentID' AND attempt = '$attempt' AND questionID ='$currentQuestion' AND id IN (SELECT MAX(id) FROM studentresponse GROUP BY studentID)";
        $result = mysqli_query($con,$query) or die ("Could not get test feedback");
        $nrows = mysqli_num_rows($result);
        for ($i = 0; $i < $nrows; $i++ )
                {
                        $row = mysqli_fetch_assoc($result);
                        extract($row);
                echo    
                        "<ul>
                        <li>Question: $questionID</li>\n
                        <li>Test Feedback/Selected Answer: $selectedAnswer</li>\n
                        <li>Test Feedback/Correct Answer: $correctAnswer</li>\n
                        <li>Feedback: var to go here</li></ul>";        
				}
		}
///////////////////////////////////////////////////////////////////////////////CHECK ATTEMPTS
function getAttempts($studentID) 
{
	$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die ("Could not execute query");
	$query = "SELECT MAX(attempt) FROM studentintest WHERE studentID ='$studentID'";
	$result = mysqli_query($con,$query) or die ("Could not check get attempt no");
	while ($row = mysqli_fetch_array($result)) 
{
	$attempt = $row[0]; 
	$_SESSION['attempt'] = $attempt;
	$attempt = $_SESSION['attempt'];
	if (is_null($attempt))
{	
	$attempt = 1;
	$_SESSION['attempt'] = $attempt;
	$attempt = $_SESSION['attempt'];
}	
	}
}
/////////////////////////////////////////////////////////////////////////////////////// CHECK ANSWER
function checkAnswer($currentQuestion) 
{
        $con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die ("Could not execute query");
        $query= "SELECT correctAnswer FROM question WHERE questionID = '$currentQuestion'";
        $result = mysqli_query($con,$query) or die ("Could not check answer in database $query");
		return mysqli_fetch_row($result);
}
/////////////////////////////////////////////////////////////////////////////// PUT STUDENT RESPONSE
function putStudentResponse($studentID, $attempt, $currentQuestion, $testID)
{
if(isset($_POST['questionOptions']))
{   $selectedAnswer = $_POST['questionOptions'];
	$checkAnswer = checkAnswer($currentQuestion);
    $correctAnswer = $checkAnswer;		
    $con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die ("Could not execute query");
if (!is_array($selectedAnswer))
{      
        $query = "INSERT INTO studentresponse (correctAnswer, attempt, studentID, selectedAnswer, questionID, testID,  startTime)"
                . " VALUES ("
				. "'" . $correctAnswer[0] . "',"
                . "'" . $attempt . "',"
                . "'" . $studentID . "',"
                . "'" . $_POST['questionOptions'] . "',"
                . "'" . $currentQuestion  . "',"
                . "'" . $testID  . "', NOW()"
                . ")";  
                $result = mysqli_query($con,$query) or die ("Could not record answer in database");

}
 else if (is_array($selectedAnswer))
{
        foreach($selectedAnswer as $answer)
        {
        $arrayAnswer = implode(",", $selectedAnswer);
}      
        $query = "INSERT INTO studentresponse (correctAnswer, attempt, studentID, selectedAnswer, questionID, testID,  startTime)"
                . " VALUES ("
				. "'" . $correctAnswer[0] . "',"
                . "'" . $attempt . "',"
                . "'" . $studentID . "',"
                . "'" . $arrayAnswer . "',"
                . "'" . $currentQuestion  . "',"
                . "'" . $testID  . "', NOW()"
				                . ")";  
                $result = mysqli_query($con,$query) or die ("Could not record answer in database $query");
        }
}
}
/////////////////////////////////////////////////////////////////////// 	START TEST  
function startTest($studentID, $currentQuestion, $testID)
{
if ($currentQuestion == 1) 
{	
	$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die ("Could not execute query");
	$query = "INSERT INTO studentintest (studentID, testID, startTime) VALUES ("
	. "'" . $studentID . "',"
	. "'" . $testID . "',"
	. "NOW()" 
	. ")";	
		$result = mysqli_query($con,$query) or die ("Could not record start of test in database");
}
}
////////////////////////////////////////////////////////////////////////////////	FINISH TEST
function finishTest($studentID, $attempt, $testID)
{	
	$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die ("Could not execute query");
	$query = "SELECT MAX(id) FROM studentintest WHERE studentID = '$studentID' and testID = '$testID'";
	$result = mysqli_query($con,$query) or die ("Could not get last start of test from database");
	while ($row = mysqli_fetch_array($result)) 
{
		$currentAttempt = $row[0]; 
}
	$attempt = $_SESSION['attempt']; 
	$score =  $_SESSION['score'];
	//$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die ("Could not execute query");
	$query = "UPDATE studentintest SET endTime="
	. "NOW()," 
	. " finalScore = " 
	. "'" . $_POST['score'] . "',"
	. " attempt = " 
	. "'" . $_SESSION['attempt'] . "'"
		. " WHERE studentID = "
	. "'" . $studentID . "' AND testID =  "
	. "'" . $testID  . "' AND id = "
	. "'" . $currentAttempt . "'";
$result = mysqli_query($con,$query) or die ("Could not record end of test in database");
//$attempt++;
//$_SESSION['attempt'] = $attempt;
$_SESSION['finished'] = true;
}
///////////////////////////////////////////////////////////////////////////////		CALCULATE % OF RIGHT ANSWERS
function percentRight($currentQuestion)
{
	$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die ("Could not execute query");
	$query = "SELECT * FROM `studentresponse` WHERE questionID='$currentQuestion' and attempt = '1'";
	$result = mysqli_query($con,$query) or die ("Could not get the number of answers");
	//return mysqli_fetch_row($result);
	$numberOfAnswers = mysqli_num_rows($result);
	$query2 = "SELECT * FROM `studentresponse` WHERE questionID='$currentQuestion' and attempt = '1' and correctAnswer = selectedAnswer";
	$result2 = mysqli_query($con,$query2) or die ("Could not get the number of right answers");
	$numberOfRight = mysqli_num_rows($result2);
	if ($numberOfRight == 0) 
	{
	$percentageRight = 0;
	}
	else
	{
	$percentageRight =  round($numberOfRight/$numberOfAnswers*100);}
	return $percentageRight;
	} 	

?>
